package com.songqintuan.platform.tree;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ValueChangeEvent;

import org.richfaces.component.UITree;
import org.richfaces.component.UITreeModelRecursiveAdaptor;
import org.richfaces.component.UITreeNode;
import org.richfaces.event.TreeSelectionChangeEvent;
import org.richfaces.event.TreeToggleEvent;

import com.songqintuan.common.constant.GlobalConstants;
import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.model.po.AuthorityResources;
import com.songqintuan.persist.service.hibernate.IAuthorityResourcesService;
import com.songqintuan.platform.common.constant.PlatformSpringBeanConstants;
@ManagedBean(name="manageResourceTree")
@SessionScoped
public class ResourceTree implements Serializable {
	 
    private static final long serialVersionUID = 2L;
 
    private UITree tree = new UITree();
    
    private ResourceTreeNode root = new ResourceTreeNode();
 
    private ResourceTreeNode nodes = new ResourceTreeNode();
 
    
    public UITree getTree() {
		return tree;
	}

	public void setTree(UITree tree) {
		this.tree = tree;
	}


    public ResourceTreeNode getRoot() {
		return root;
	}

	public void setRoot(ResourceTreeNode root) {
		this.root = root;
	}

	public ResourceTreeNode getNodes() {
		return nodes;
	}

	public void setNodes(ResourceTreeNode nodes) {
		this.nodes = nodes;
	}
    public ResourceTree() {
    	structureTree();
    }
 
    public void structureTree() {
    	tree.setData(root);
    	//构造根节点
    	root.setName("资源管理");
    	root.setId("0");
    	root.setCheckboxEnabled(false);
    	root.setSelected(false);
    	nodes.addChild(root.getId(), root);
    	createChilds(root);

 
 
    }
    
    private void createChilds(ResourceTreeNode currTreeNode){
    	@SuppressWarnings("unchecked")
		IAuthorityResourcesService<AuthorityResources, Long> authorityResourcesService = (IAuthorityResourcesService<AuthorityResources, Long>) SpringFacesUtil.getSpringBean(PlatformSpringBeanConstants.AUTHORITY_RESOURCES_SERVICE_BEAN_NAME);
		authorityResourcesService.setEntityClass(AuthorityResources.class);
		authorityResourcesService.setPKClass(Long.class);
		List<AuthorityResources> resources = authorityResourcesService.findBy("pid", new Long(currTreeNode.getId()), "sort", GlobalConstants.ORDER_BY_ASC_TRUE);
    	Iterator itChirldList = resources.iterator();
    	while (itChirldList.hasNext()) {
    		AuthorityResources resource = (AuthorityResources) itChirldList.next();
    		//判断是否有子菜单，好生成子节点
    		List<AuthorityResources> himChildren = authorityResourcesService.findBy("pid", new Long(resource.getId()), "sort", GlobalConstants.ORDER_BY_ASC_TRUE);
    		boolean himleaf = himChildren.isEmpty()?true:false;
    		ResourceTreeNode child = new ResourceTreeNode(resource.getId(),resource.getName(),himleaf, false, false);
    		currTreeNode.addChild(resource.getId() + resource.getPid(), child);
    	}
    }
    public void selectionChanged(TreeSelectionChangeEvent event) throws AbortProcessingException {
    	List<Object> selection = new ArrayList<Object>(event.getNewSelection());
        Object currentSelectionKey = selection.get(0);
        UITree tree = (UITree) event.getSource();
 
        Object storedKey = tree.getRowKey();
        tree.setRowKey(currentSelectionKey);
        ResourceTreeNode currentSelection = (ResourceTreeNode) tree.getRowData();
        tree.setRowKey(storedKey);
  
    	System.out.println(currentSelection.getName());
    	}
   public void changeTreeNodeVaue(ValueChangeEvent event){
	   String dddd =(String) event.getNewValue();
	   System.out.println(dddd);
   }

	public void processTreeToggle(TreeToggleEvent event) {
		UITreeNode node = (UITreeNode) event.getSource();
		ResourceTreeNode selectedNode = null;
		UITree tree = (UITree) node.getParent();
		selectedNode = (ResourceTreeNode) tree.getRowData();
		if (event.isCollapsed()) {
			//createChilds(selectedNode);
		}
		if (event.isExpanded()) {
			while (selectedNode.getChildrenKeysIterator().hasNext()) {
				selectedNode.removeChild(selectedNode.getChildrenKeysIterator().next());
			}
			createChilds(selectedNode);
		}
	}

   public String submitAction(){
	   Iterator<Object> organisationRootI = this.getNodes().getChildrenKeysIterator();
	   while (organisationRootI.hasNext()){
		   ResourceTreeNode aaa= (ResourceTreeNode)this.getNodes().getChild(organisationRootI.next());
		   if (aaa.isSelected()){
			   System.out.println(aaa.getName());
		   }
		   getChildSelected(aaa);
	   }
	   return null;
   }
   public void getChildSelected(ResourceTreeNode aaa){
	   if (!aaa.isLeaf()) {
		   Iterator<Object> organisationRootI = aaa.getChildrenKeysIterator();
		   while (organisationRootI.hasNext()) {
			   ResourceTreeNode ccc = (ResourceTreeNode) aaa.getChild(organisationRootI.next());
		   if (ccc.isSelected()){
			   System.out.println(ccc.getName());
		   }
		   if (!ccc.isLeaf()) {
			   getChildSelected(ccc);
		   }
		   }
		   
	   }
   }
}
